<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="文章类型名称" prop="typeName">
        <el-input v-model="dataForm.typeName" placeholder="文章类型名称"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    getWxArticleTypeObj,
    addWxArticleTypeObj,
    putWxArticleTypeObj
  } from '@/api/mp/wxarticletype'

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          typeName: '',
          createTime: '',
          createUser: '',
          updateTime: '',
          updateUser: '',
          delFlag: '',
          tenantId: ''
        },
        dataRule: {
          typeName: [{
            required: true,
            message: '文章类型名称不能为空',
            trigger: 'blur'
          }],
          createTime: [{
            required: true,
            message: '创建时间不能为空',
            trigger: 'blur'
          }],
          createUser: [{
            required: true,
            message: '创建人不能为空',
            trigger: 'blur'
          }],
          updateTime: [{
            required: true,
            message: '更新时间不能为空',
            trigger: 'blur'
          }],
          updateUser: [{
            required: true,
            message: '修改人不能为空',
            trigger: 'blur'
          }],
          delFlag: [{
            required: true,
            message: '删除标识(1-未删除,-1已删除)不能为空',
            trigger: 'blur'
          }],
          tenantId: [{
            required: true,
            message: '租户Id不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getWxArticleTypeObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putWxArticleTypeObj(this.dataForm).then(data => {
                this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              });
            } else {
              addWxArticleTypeObj(this.dataForm).then(data => {
                this.$message.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              })
            }
          }
        })
      }
    }
  }
</script>
